package logic

import (
	"context"
	"user-reg-mgr/goods/goods"
	"user-reg-mgr/goods/internal/svc"
	"user-reg-mgr/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *goods.LoginRequest) (*goods.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	data := mysql.User{
		UserName: in.Username,
		Password: in.Password,
	}
	err := mysql.DB.Where("username=?", in.Username).Find(&data)
	if err == nil {
		panic("账号错误")
	}
	err = mysql.DB.Where("password=?", in.Password).Find(&data)
	if err == nil {
		panic("密码错误")
	}

	return &goods.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
